package icu.lookyousmileface.review;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author starrysky
 * @title: ReviewLock
 * @projectName Juc_Pro
 * @description: TODO
 * @date 2021/1/2912:58 上午
 */
public class ReviewLock {
    public static void main(String[] args) {
        //资源类
        SalveTicks salveTicks = new SalveTicks();
        //线程
        new Thread(()->{ for (int i = 0; i < 108; i++) {salveTicks.sale();}},"A").start();
        new Thread(()->{ for (int i = 0; i < 108; i++) {salveTicks.sale();}},"B").start();
        new Thread(()->{ for (int i = 0; i < 108; i++) {salveTicks.sale();}},"C").start();

    }
}
class SalveTicks{
    private int tick_num=99;
    //锁放外卖，里面每次的锁都不一样，无法锁
    Lock lock =new ReentrantLock();
    public void sale(){
        lock.lock();
        try {
            if (tick_num>0){
                System.out.println(Thread.currentThread().getName()+":"+(tick_num--)+"还剩："+tick_num);
            }
        }finally {
            lock.unlock();
        }

    }
}
